package com.yanqu.road.server.manager.activity.xiuxian.activity;

import com.google.protobuf.AbstractMessage;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.xiuxian.config.XiuXianConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.bussiness.activity.XiuXianActivityBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.XiuXianProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityInfoMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.AgentProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 配置相关
 */
public class CrossXiuXianBaseActivity {

    protected static Logger logger = LogManager.getLogger(CrossXiuXianBaseActivity.class.getName());

    /**
     * 锁
     */
    protected final Object lock = new Object();

    /**
     * 活动配置
     */
    protected XiuXianConfig config;

    /**
     * 榜单长度
     */
    protected int userMaxRankConfig;

    protected int campMaxRankConfig;



    public CrossXiuXianBaseActivity(ActivityInfo activityInfo) {
        // 读配置
        loadConfigFromDb(activityInfo);
    }

    public void loadConfigFromDb(ActivityInfo activityInfo) {
        initMaxRankConfig(activityInfo.getActivityId());

        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(Collections.singletonList(activityInfo.getActivityId())).get(activityInfo.getActivityId());
        // 读配置
        XiuXianConfig tmpConfig = new XiuXianConfig();
        tmpConfig.setActivityInfo(ActivityBussiness.getActivityInfoByActivityId(activityInfo.getActivityId()));
        tmpConfig.setPlaceConfigMap(XiuXianActivityBusiness.getXiuXianPvpPlaceConfigMap(activityInfo.getActivityId()));
        tmpConfig.setEquipmentConfigMap(XiuXianActivityBusiness.getXiuXianEquipmentConfigMap(activityInfo.getActivityId()));
        tmpConfig.setEventConfigMap(XiuXianActivityBusiness.getXiuXianEventConfigMap(activityInfo.getActivityId()));
        tmpConfig.setMissionConfigMap(XiuXianActivityBusiness.getXiuXianMissionMap(activityInfo.getActivityId()));
        tmpConfig.setStateConfigMap(XiuXianActivityBusiness.getXiuXianStateConfigMap(activityInfo.getActivityId()));
        tmpConfig.setTripodConfigMap(XiuXianActivityBusiness.getXiuXianTripodConfigMap(activityInfo.getActivityId()));
        tmpConfig.setNpcConfigMap(XiuXianActivityBusiness.getXiuXianNpcConfigMap(activityInfo.getActivityId()));
        tmpConfig.setPeakConfigMap(XiuXianActivityBusiness.getXiuXianPeakConfigMap(activityInfo.getActivityId()));
        tmpConfig.initConfig(activityConfigMap);
        config = tmpConfig;
    }

    public XiuXianConfig getConfig() {
        return config;
    }

    private void initMaxRankConfig(int activityId) {
        userMaxRankConfig = getMaxRankConfig(ActivityInfoMgr.getConditionInfoList(activityId, eGamePlayerEventType.XiuXianPersonalScoreRank.getValue()));
        campMaxRankConfig = getMaxRankConfig(ActivityInfoMgr.getConditionInfoList(activityId, eGamePlayerEventType.XiuXianSectScoreRank.getValue()));
    }

    private int getMaxRankConfig(List<ActivityConditionInfo> list) {
        int maxRank = 0;
        for (ActivityConditionInfo conditionInfo : list) {
            int tmp = conditionInfo.getParamList().get(1).intValue();
            if (tmp > 0 && maxRank < tmp) {
                maxRank = tmp;
            }
        }
        return maxRank;
    }


    protected void sendPacket(int code, AbstractMessage.Builder<?> messageBuilder, long userId) {
        UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(userId);
        if (userBaseInfo == null) {
            return;
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(code, messageBuilder);
        MessageHelper.sendPacket(userBaseInfo.getServerId(), userId, message);
    }

    protected void sendPacketToAll(int code, AbstractMessage.Builder<?> messageBuilde, Collection<Long> userIdList) {
        XiuXianProto.XiuXianAgentTransferMsg.Builder agentMsg = XiuXianProto.XiuXianAgentTransferMsg.newBuilder();
        agentMsg.setCode(code);
        agentMsg.addAllUserId(userIdList);
        agentMsg.setMsg(messageBuilde.build().toByteString());

        YanQuMessage message = YanQuMessageUtils.buildMessage(AgentProtocol.A_XIU_XIAN_TRANSFER, agentMsg);
        for (Long serverId : config.getActivityInfo().getServerIdList()) {
            MessageHelper.sendPacket(serverId, 0, message);
        }
    }
}
